<?php

namespace Ad\Logic\Qianlu;

use \common\Table\InputClick\AdClickQianluTable;
use \common\Table\InputClick\AdClickTable;
use \core\filter\filter;
use \common\Factory\Factory;
use \core\data\partitionTable;
use \Ad\Logic\baseInputClick;

class QianluInputClick extends baseInputClick {
    
    public function insert($data) {
        $checkIpResult = $this->checkIp('qianlu');
        if($checkIpResult === false) return array('success' => false, 'message' => 'ip check failed');
        
        Factory::getServer('MqWriteLog')->send('qianlu click raw input params:' . json_encode($data));
        $config = \core\init\init::getConfig();
        
        //验证及过滤
        $adQianluClickColumns = array(
            'appid' => FILTER_SANITIZE_STRING,
            'idfa' => FILTER_SANITIZE_STRING, 
            'mac' => FILTER_SANITIZE_STRING
        );
        $adClickColumns = array('idfa' => FILTER_SANITIZE_STRING, 'appid' => FILTER_SANITIZE_STRING);
        $filter = new filter();
        $adQianluClickColumns = $filter->varArray($data, $adQianluClickColumns);
        $adClickColumns = $filter->varArray($data, $adClickColumns);
        
        //sign检查
//         $signString = '';
//         foreach($data as $key => $d) {
//             $key != 'sign' && $signString .= $key . '=' . $d;
//         }

//         $signOk = md5($signString . $config['adClickToken']['qianlu']) == $data['sign'] ? true : false;
//         $signOk === false && Factory::getServer('MqWriteLog')->send('qianlu click sign validate error:' . json_encode($data));

        $signOk = true;
        $result = false;
        if($adQianluClickColumns !== false && $adClickColumns !== false && $signOk === true) {
            //合并额外参数
            $timestamp = time();
            $adClickColumns['app_id'] = $adClickColumns['appid'];
            unset($adClickColumns['appid']);
            $adQianluClickColumns = array_merge($adQianluClickColumns, array('input_timestamp' => $timestamp));
            $adClickColumns = array_merge($adClickColumns, array('type' => $config['adType']['qianlu'], 'time' => date('Ym', $timestamp), 'input_timestamp' => $timestamp));
            
            //入库
            $adClickTable = new AdClickTable();
            $adClickQianluTable = new AdClickQianluTable();
            $db = $adClickQianluTable->getDb();
            
            $partitionTable = new partitionTable();
            $db->beginTransaction();
            $tableNum = $partitionTable->getHashTable($adQianluClickColumns['idfa']);
            $qiankaResult = $adClickQianluTable->insertAdQianluClick($adQianluClickColumns, $tableNum);
            if($qiankaResult === true) {
                $result = $adClickTable->insertAdClick($adClickColumns);
                if($result === true) {
                    $db->commit();
                    Factory::getServer('MqWriteLog')->send('qianlu click qianlu_click table insert success');
                }
                else {
                    $db->rollBack();
                    Factory::getServer('MqWriteLog')->send('qianlu click ad_click table insert failed');
                }
            }
            else {
                $db->rollBack();
                Factory::getServer('MqWriteLog')->send('qianlu click qianlu_click table insert failed');
            }                
        }
        else {
            Factory::getServer('MqWriteLog')->send('qianlu click raw input params validate failed');
        }
        
        if($result === true) {
            $result = array('success' => true, 'message' => 'ok'); 
        }
        else {
            Factory::getServer('MqWriteLog')->send('qianlu click failed');
            $result = array('success' => false, 'message' => '失败');
        }
        
        return $result;
    }
}